package com.zlt.util;


import com.zlt.entity.UserVip;
import com.zlt.entity.Vip;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {


    //判断日期是否过期
    public static boolean isNotExpired(String endTime) throws ParseException {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date currentTime = new Date();

        Date end = simpleDateFormat.parse(endTime);

        int i = end.compareTo(currentTime);//截止时间与当前时间作比较   返回的值 小于等于0 就过期了
//        System.out.println(i);
        boolean bool = true;
        if (i<=0){
            bool = false;
        }

        return bool;


    }


    public static String getCurrentDateString(){

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format = sdf.format(date);
//        System.out.println(format);

        return format;
    }

    //vip的续期
    public static String addEndTime(String originEndTime, Vip vip) {


        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date dt= null;
        try {
            dt = sdf.parse(originEndTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println("Date:"+dt);


        Calendar rightNow = Calendar.getInstance();
        System.out.println("rightNow:"+rightNow);
        rightNow.setTime(dt);//设置日期


        System.out.println(rightNow);
        System.out.println("============");
//        rightNow.add(Calendar.YEAR,-1);//日期减1年
        rightNow.add(Calendar.MONTH,vip.getPriority());//日期加3个月
//        rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天

        Date dt1=rightNow.getTime();

        String reStr = sdf.format(dt1);
        System.out.println(reStr);

        return reStr;
    }


    //新用户刚注册的时候  或者之前已经过期的用户  再次充值  就新增一条
    //充值记录
    public static String endTime(UserVip userVip, Vip vip) {


        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date dt= null;
        try {
            dt = sdf.parse(userVip.getStartTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println("Date:"+dt);


        Calendar rightNow = Calendar.getInstance();
        System.out.println("rightNow:"+rightNow);
        rightNow.setTime(dt);//设置日期


        System.out.println(rightNow);
        System.out.println("============");
//        rightNow.add(Calendar.YEAR,-1);//日期减1年
        rightNow.add(Calendar.MONTH,vip.getPriority());//日期加3个月
//        rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天

        Date dt1=rightNow.getTime();

        String reStr = sdf.format(dt1);
        System.out.println(reStr);

        return reStr;
    }


    //更新用户vip的最新状态（已过期就更改为过期状态）


    public static java.util.Date parse(java.sql.Date date) {
        long time = date.getTime();
        return new Date(time);
    }

    public static String format(java.util.Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    public static java.sql.Date parse(java.util.Date date) {
        long time = date.getTime();
        return new java.sql.Date(time);
    }





}
